package com.yanqu.road.dao.impl.wingroom.chakra;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.wingroom.chakra.config.WingRoomChakraAttributeConfig;
import com.yanqu.road.entity.wingroom.chakra.config.WingRoomChakraChartConfig;
import com.yanqu.road.entity.wingroom.chakra.config.WingRoomChakraConfig;
import com.yanqu.road.entity.wingroom.chakra.config.WingRoomChakraWindWaterConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WingRoomChakraConfigDaoImpl extends TempDao {

    /**
     * 获取星盘配置LIST
     */
    private static String getWingRoomChakraConfigSql = "select * from `t_p_wingroom_chakra`";
    public Map<Integer, Map<Integer, WingRoomChakraConfig>> getWingRoomChakraConfigMap() {
        Map<Integer, Map<Integer, WingRoomChakraConfig>> map = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getWingRoomChakraConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                WingRoomChakraConfig wingRoomChakraConfig = new WingRoomChakraConfig();
                wingRoomChakraConfig.setId(rs.getInt("id"));
                wingRoomChakraConfig.setType(rs.getInt("type"));
                wingRoomChakraConfig.setSort(rs.getInt("sort"));
                wingRoomChakraConfig.setLittleType(rs.getInt("littleType"));
                Map<Integer, WingRoomChakraConfig> positionMap = map.computeIfAbsent(wingRoomChakraConfig.getType(), k -> new HashMap<>());
                positionMap.put(wingRoomChakraConfig.getSort(), wingRoomChakraConfig);
            }
            return map;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    /**
     * 获取风水值配置
     */
    private static String getWingRoomWindChakraWaterConfigSql = "select * from `t_p_wingroom_chakra_wind_water`";
    public Map<Integer, WingRoomChakraWindWaterConfig> getWingRoomWindChakraWaterConfigMap() {
        Map<Integer, WingRoomChakraWindWaterConfig> map = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getWingRoomWindChakraWaterConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                WingRoomChakraWindWaterConfig wingRoomWindChakraWaterConfig = new WingRoomChakraWindWaterConfig();
                wingRoomWindChakraWaterConfig.setId(rs.getInt("id"));
                String weightListStr = rs.getString("weight");
                List<Integer> weightList = StringUtils.stringToIntegerList(weightListStr,"\\|");
                wingRoomWindChakraWaterConfig.setWeightList(weightList);
                wingRoomWindChakraWaterConfig.setEntryWeight(rs.getString("entryWeight"));
                wingRoomWindChakraWaterConfig.setGeomancyId(rs.getLong("geomancyId"));
                map.put(wingRoomWindChakraWaterConfig.getId(), wingRoomWindChakraWaterConfig);
            }
            return map;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    /**
     * 获取风水值配置
     */
    private static String getWingRoomWindChakraWaterConfigSql2 = "select * from `t_p_wingroom_chakra_wind_water` order by id asc";
    public List<WingRoomChakraWindWaterConfig> getWingRoomWindChakraWaterConfigList() {
        List<WingRoomChakraWindWaterConfig> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getWingRoomWindChakraWaterConfigSql2);
            rs = call.executeQuery();
            while (rs.next()) {
                WingRoomChakraWindWaterConfig wingRoomWindChakraWaterConfig = new WingRoomChakraWindWaterConfig();
                wingRoomWindChakraWaterConfig.setId(rs.getInt("id"));
                String weightListStr = rs.getString("weight");
                List<Integer> weightList = StringUtils.stringToIntegerList(weightListStr,"\\|");
                wingRoomWindChakraWaterConfig.setWeightList(weightList);
                wingRoomWindChakraWaterConfig.setEntryWeight(rs.getString("entryWeight"));
                wingRoomWindChakraWaterConfig.setGeomancyId(rs.getLong("geomancyId"));
                list.add(wingRoomWindChakraWaterConfig);
            }
            return list;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }


    /**
     * 获取命盘等级配置
     */
    private static String getWingRoomChakraChartConfigSql = "select * from `t_p_wingroom_chakra_chart`";
    public Map<Integer, WingRoomChakraChartConfig> getWingRoomChakraChartConfigMap() {
        Map<Integer, WingRoomChakraChartConfig> map = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getWingRoomChakraChartConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                WingRoomChakraChartConfig wingRoomChakraChartConfig = new WingRoomChakraChartConfig();
                wingRoomChakraChartConfig.setId(rs.getInt("id"));
                wingRoomChakraChartConfig.setExp(rs.getInt("exp"));
                wingRoomChakraChartConfig.setConsume(rs.getInt("consume"));
                wingRoomChakraChartConfig.setParam(rs.getString("param"));
                map.put(wingRoomChakraChartConfig.getId(), wingRoomChakraChartConfig);
            }
            return map;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    /**
     * 获取命盘属性配置
     */
    private static String getWingRoomChakraAttributeConfigSql = "select * from `t_p_wingroom_chakra_attribute`";
    public Map<Integer, Map<Integer, WingRoomChakraAttributeConfig>> getWingRoomChakraAttributeConfigMap() {
        Map<Integer, Map<Integer, WingRoomChakraAttributeConfig>> map = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getWingRoomChakraAttributeConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                WingRoomChakraAttributeConfig wingRoomChakraAttributeConfig = new WingRoomChakraAttributeConfig();
                wingRoomChakraAttributeConfig.setId(rs.getInt("id"));
                wingRoomChakraAttributeConfig.setType(rs.getInt("type"));
                wingRoomChakraAttributeConfig.setLevel(rs.getInt("level"));
                wingRoomChakraAttributeConfig.setEntryAttribute(StringUtils.stringToIntegerList(rs.getString("entryAttribute"), "\\|"));
                wingRoomChakraAttributeConfig.setIncrease(StringUtils.stringToIntegerList(rs.getString("increase"), "\\|"));
                Map<Integer, WingRoomChakraAttributeConfig> levelMap = map.computeIfAbsent(wingRoomChakraAttributeConfig.getType(), k -> new HashMap<>());
                levelMap.put(wingRoomChakraAttributeConfig.getLevel(), wingRoomChakraAttributeConfig);
            }
            return map;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }
}
